﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Delay;

namespace lfm.Parser
{
    internal class Event
    {
        public static async Task<lfm.Event> getInfo(String e)
        {
            lfm.Event result = new lfm.Event();

            // Make Method Run Asynchronously
            await Task.Run(() =>
            {
                String r = e;
                r = r.Replace("geo:point", "point");
                r = r.Replace("geo:lat", "lat");
                r = r.Replace("geo:long", "long");

                XDocument x = XDocument.Parse(r);

                // Parse the similar artists list
                result = (from u in x.Descendants("event")
                             select new lfm.Event
                             {
                                 ID = u.SafeGetElement("id").SafeGetInt64Value(),
                                 Title = u.SafeGetElement("title").SafeGetStringValue(),
                                 StartDate = u.SafeGetElement("startdate").SafeGetDateTimeValue(DateTime.Now),
                                 EndDate = u.SafeGetElement("enddate").SafeGetDateTimeValue(DateTime.Now),
                                 Description = u.SafeGetElement("description").SafeGetStringValueEmptyable(),
                                 ImageSmall = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "small").FirstOrDefault().SafeGetUriValue(),
                                 ImageMedium = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "medium").FirstOrDefault().SafeGetUriValue(),
                                 ImageLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "large").FirstOrDefault().SafeGetUriValue(),
                                 ImageExtraLarge = u.Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "extralarge").FirstOrDefault().SafeGetUriValue(),
                                 Attendance = u.SafeGetElement("attendance").SafeGetInt64Value(),
                                 Reviews = u.SafeGetElement("reviews").SafeGetInt64Value(),
                                 Tag = u.SafeGetElement("tag").SafeGetStringValue(),
                                 URL = u.SafeGetElement("url").SafeGetUriValue(),
                                 Website = u.SafeGetElement("website").SafeGetUriValue(),
                                 Tickets = u.SafeGetElement("tickets").SafeGetUriValue(),
                                 Cancelled = Converters.ToBool(u.SafeGetElement("url").SafeGetStringValue()),
                                 Venue = new Venue
                                 {
                                     ID = u.SafeGetElement("venue").SafeGetElement("id").SafeGetInt64Value(),
                                     Name = u.SafeGetElement("venue").SafeGetElement("name").SafeGetStringValue(),
                                     City = u.SafeGetElement("venue").SafeGetElement("location").SafeGetElement("city").SafeGetStringValue(),
                                     Country = u.SafeGetElement("venue").SafeGetElement("location").SafeGetElement("country").SafeGetStringValue(),
                                     Street = u.SafeGetElement("venue").SafeGetElement("location").SafeGetElement("street").SafeGetStringValue(),
                                     PostalCode = u.SafeGetElement("venue").SafeGetElement("location").SafeGetElement("postalcode").SafeGetStringValue(),
                                     Latitude = u.SafeGetElement("venue").SafeGetElement("location").SafeGetElement("point").SafeGetElement("lat").SafeGetStringValue(),
                                     Longtitude = u.SafeGetElement("venue").SafeGetElement("location").SafeGetElement("point").SafeGetElement("long").SafeGetStringValue(),
                                     URL = u.SafeGetElement("venue").SafeGetElement("url").SafeGetUriValue(),
                                     Website = u.SafeGetElement("venue").SafeGetElement("website").SafeGetUriValue(),
                                     PhoneNumber = u.SafeGetElement("venue").SafeGetElement("phonenumber").SafeGetStringValue(),
                                     ImageSmall = u.SafeGetElement("venue").Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "small").FirstOrDefault().SafeGetUriValue(),
                                     ImageMedium = u.SafeGetElement("venue").Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "medium").FirstOrDefault().SafeGetUriValue(),
                                     ImageLarge = u.SafeGetElement("venue").Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "large").FirstOrDefault().SafeGetUriValue(),
                                     ImageExtraLarge = u.SafeGetElement("venue").Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "extralarge").FirstOrDefault().SafeGetUriValue(),
                                     ImageMega = u.SafeGetElement("venue").Elements("image").Where(n => n.SafeGetAttribute("size").SafeGetStringValue() == "mega").FirstOrDefault().SafeGetUriValue()
                                 },
                                 TempArtists = (from a in u.Element("artists").Descendants("artist") select new lfm.Artist { Name = a.SafeGetStringValue() }),
                                 Artists = new List<String>(),
                                 Headliner = u.SafeGetElement("artists").SafeGetElement("headliner").SafeGetStringValue()
                             }).FirstOrDefault();

                
                    // Populate the artists list
                    foreach (var a in result.TempArtists)
                    {
                        if (a != null) result.Artists.Add(a.Name);
                    }
            });

            // return the full artist
            return result;
        }
    }
}
